package jzoffer;

/**
 * @author MaoLin Wang
 * @date 2020/10/8 9:56 上午
 */
public class _25合并两个有序链表 {
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1==null){
            return l2;
        }
        if (l2==null){
            return l1;
        }
        ListNode newnode=new ListNode(-1);
        ListNode node1=l1,node2=l2;
        ListNode temp=null;
        ListNode tail=newnode;
        while (node1!=null && node2 !=null){
            if (node1.val<node2.val){
                temp=node1;
                node1=node1.next;
            }else {
                temp=node2;
                node2=node2.next;
            }
            tail.next=temp;
            tail=tail.next;

        }
        if (node1!=null){
            tail.next=node1;
        }
        if (node2!=null){
            tail.next=node2;
        }
        return newnode.next;
    }

    public static void main(String[] args) {

        ListNode node=new ListNode(1);
        node.next=new ListNode(2);
        node.next.next=new ListNode(3);

        ListNode node1=new ListNode(2);
        node1.next=new ListNode(5);
        node1.next.next=new ListNode(7);
        ListNode node2 = mergeTwoLists(node1, node);
        node2.print();
    }
}
